#include<iostream>
#include<vector>

using namespace std;

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        if (amount == 0)return 0;
        int length = coins.size();
        vector<int> dp(amount + 1, 0);
        for (int i = 0; i < length; i++) {
            for (int j = 1; j <= amount; j++) {
                int sz = j - coins[i];
                if (sz == 0) {
                    dp[j] = 1;
                }
                else if (sz > 0 && dp[sz]) {
                    if (dp[j] == 0)
                    {
                        dp[j] = dp[sz] + 1;
                    }
                    else {
                        dp[j] = min(dp[j], dp[sz] + 1);
                    }

                }
            }
        }
        return dp[amount] ? dp[amount] : -1;
    }
};